// pages/article/Articles.js
var Zan = require('../../dist/index.js');
let WxNotificationCenter = require('../../utils/WxNotificationCenter.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;

Page(Object.assign({}, Zan.Tab, Zan.TopTips, {
  /**
   * 页面的初始数据
   */
  data: {
    tab: {},
    // menu: [],
    title: '',
    article: [],
    recordsTotal: 0,
    // typeId: null,
    orderNO: 0,
    radioCheckVal: 0,
    selectedId: 0,
    //    canReceive: true
    nodata: {
      msg: '该文章不存在',
      notype: 2,
      imgurl: '../../images/nodatas.png',
      nourl: ''
    },
    nodata_s: {
      msg: '该文章不存在',
      notype: 2,
      imgurl: '../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    // var menu = {};
    // menu.id = null;
    // menu.name = '所有';
    // menu.orderNo = 0;
    // this.setData({
    //   menu: this.data.menu.concat(menu),
    //   typeId: options.linkId
    // })
    // 注册列表刷新事件
    WxNotificationCenter.addNotification("articleRefreshNotification", this.refreshNotification, this);
    
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      article: [],
      // typeId: this.data.menu[this.data.selectedId].id
    });

    network._post_page_json(this, api.getArticle, start, { }, success);

    // var menu = {};
    // menu.id = null;
    // menu.name = '所有';
    // menu.orderNo = 0;
    // var m = [menu];
    // this.setData({
    //   menu: m,
    // })

    // network._post_json(this, api.getAriticleTypeList, {}, successType);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.article.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.getArticle, start, {  }, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  refreshNotification:function(e){
    this.onPullDownRefresh();
  },
  /**
    * 进入搜索页面
    */
  cancel: function () {
    this.setData({
      title: ''
    })
  },

  /**
   * 选择类型
   */
  // radioCheckedChange: function (e) {
  //   this.setData({
  //     radioCheckVal: e.detail.value,
  //     typeId: this.data.menu[e.detail.value].id,
  //     activeIndex: 10
  //   })
  //   this.onPullDownRefresh();
  // },

  /**
  * 选择排序
  */
  checkedChangeOrder: function (e) {
    this.setData({
      radioCheckOrderVal: e.detail.value,
      orderNO: e.detail.value,
      activeIndex: 10
    })
    this.onPullDownRefresh();
  },

  /**
   * 点击商品进入商品详情
   */
  onArticleItemClick: function (e) {
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/article/ArticleDetails?articleId=' + item.id
    })
  },

  /**
  * 
  */
  // handleZanTabChange(e) {
  //   var componentId = e.componentId;
  //   var selectedId = e.selectedId;

  //   this.setData({
  //     article: [],
  //     selectedId: selectedId,
  //     [`${componentId}.selectedId`]: selectedId
  //   });

  //   start = 0;
  //   network._post_product_request(this, api.getAriticleList, start, { typeId: this.data.menu[selectedId].id }, success);
  // },

  //进入购物车
  go_shopcar: function (e) {
    wx.navigateTo({
      url: '/pages/shoppingCar/ShoppingCar',
    })
  },

  //进入搜索页面
  goArticleSearch: function (e) {
    wx.navigateTo({
      url: '/pages/article/ArticleSearch'
    })
  }
}))

// 获取商品分类结果
// var successType = function (that, res) {
//   if (res.data.data) {
//     that.setData({
//       menu: that.data.menu.concat(res.data.data)
//     })

//     var menuItems = []
//     for (var i = 0; i < that.data.menu.length; i++) {
//       var menuItem = {};
//       menuItem.id = i;
//       menuItem.title = that.data.menu[i].name;
//       menuItems.push(menuItem);
//     }

//     var index = that.data.selectedId;
//     for (var i = 0; i < that.data.menu.length; i++) {
//       if (that.data.menu[i].id == that.data.typeId) {
//         index = i;
//       }
//     }

//     that.setData({
//       tab: {
//         list: menuItems,
//         scroll: false,
//         selectedId: index
//       }
//     })
//   }
// }

// 获取商品列表结果
var success = function (that, res) {
  // for (let j = 0; j < res.data.data.length; j++) {
  //   res.data.data[j].createDate = utils.formatDate06(new Date(res.data.data[j].createDate))
  // }
  
  if (res.data.data) {
    that.setData({
      article: that.data.article.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
    // console.log("=========recordsTotal========");
    // console.log(that.data.recordsTotal);
  }
}
